import { postJoinProject, postIslandJoinProject } from '../../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    current: {
      type: Object,
      value: {}
    },
    detail: {
      type: Object,
      value: {}
    }
  },
  observers: {},
  data: {
    showProjectType: false,
    showProject: false,
    showSpace: false,
    showStyleModal: false,
    form: {
      type: '',
      typeStr: '',
      projectId: '',
      projectName: '',
      spaceId: [],
      spaceStr: '',
      appliedArea: '',
      businessType: '',
      businessTypeStr: ''
    }
  },
  methods: {
    changeInput ({ currentTarget: { dataset: { key } }, detail }) {
      this.setData({ ['form.' + key]: detail.value })
    },
    async selectSpace () {
      // await wx.$pro.hideKeyboard()
      if (!this.data.form.projectId) {
        wx.showToast({ title: '请先选择项目！', icon: 'none' })
        return
      }
      if (parseInt(this.data.form.type) === 2 && !this.data.form.businessType) {
        wx.showToast({ title: '请先选择业态！', icon: 'none' })
        return
      }
      this.setData({ showSpace: true })
    },
    async selectProject () {
      // await wx.$pro.hideKeyboard()
      if (!this.data.form.type) {
        wx.showToast({ title: '请先选择项目类型！', icon: 'none' })
        return
      }
      this.setData({ showProject: true })
    },
    selectType () {
      this.setData({ showProjectType: true })
    },
    async selectStyle () {
      // await wx.$pro.hideKeyboard()
      if (!this.data.form.projectId) {
        wx.showToast({ title: '请先选择项目！', icon: 'none' })
        return
      }
      this.setData({ showStyleModal: true })
    },
    close () {
      this.triggerEvent('close')
    },
    beforeOpen () {
      let specKeys = this.data.current.specKey.split('_')
      this.data.detail.spec.map((item, index) => item.specItem.map((v, num) => {
        let rowIndex = specKeys.findIndex(row => parseInt(row) === v.id)
        if (rowIndex > -1) item.activeId = v.id
      }))
      this.data.form = {
        type: '',
        typeStr: '',
        projectId: '',
        projectName: '',
        spaceId: [],
        spaceStr: '',
        appliedArea: '',
        businessType: '',
        businessTypeStr: ''
      }
      this.setData({ 'detail.spec': this.data.detail.spec, form: this.data.form })
    },
    closeTypePop () {
      this.setData({ showProjectType: false })
    },
    changeType ({ detail }) {
      this.setData({ 'form.type': detail.value, 'form.typeStr': detail.label, showProjectType: false })
    },
    closeProjectPop () {
      this.setData({ showProject: false })
    },
    changeProject ({ detail }) {
      if (parseInt(this.data.form.type) === 1) {
        this.setData({ 'form.projectId': detail.projectId, 'form.projectName': detail.projectName, showProject: false })
      } else {
        this.setData({ 'form.projectId': detail.id, 'form.projectName': detail.name, 'form.roleIds': detail.role_ids, 'form.business_type': detail.business_type, showProject: false })
      }
    },
    closeSpacePop () {
      this.setData({ showSpace: false })
    },
    changeSpace ({ detail }) {
      let ids = []
      let idsStr = []
      detail.forEach(item => {
        if (item.isSelect) {
          ids.push(item.spaceId)
          idsStr.push(item.spaceName)
        }
      })
      this.setData({ 'form.spaceId': ids, 'form.spaceStr': idsStr.join(','), showSpace: false })
    },
    closeStylePop () {
      this.setData({ showStyleModal: false })
    },
    changeStyle ({ detail }) {
      this.setData({ 'form.businessType': detail.id, 'form.businessTypeStr': detail.title, showStyleModal: false })
    },
    async confirm () {
      try {
        if (!this.data.form.type) {
          wx.showToast({ title: '请先选择项目类型！', icon: 'none' })
          return
        }
        if (!this.data.form.projectId) {
          wx.showToast({ title: '请先选择项目！', icon: 'none' })
          return
        }
        if (this.data.form.type === 2 && !this.data.form.businessType) {
          wx.showToast({ title: '请先选择业态！', icon: 'none' })
          return
        }
        if (!this.data.form.spaceId.length) {
          wx.showToast({ title: '请先选择使用空间！', icon: 'none' })
          return
        }
        let query = { ...this.data.form }
        query.specKey = this.data.current.specKey
        query.materialId = this.data.current.materialId
        if (parseInt(query.type) === 1) {
          await postJoinProject({ ...query })
        } else {
          await postIslandJoinProject({ ...query })
        }
        wx.showToast({ title: '加入项目成功！', icon: 'none' })
        this.close()
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    }
  },
  ready () {}
})